﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web.Security;
using AppLab.Core;
using AppLab.Data.Models;
using AppLab.Data.Resources;
using AppLab.Web.Resources;

namespace AppLab.Web
{
    public sealed partial class PersonalSetting
    {
        [Key]
        [Display(AutoGenerateField = false)]
        public string UserName { set; get; }

        [Display(AutoGenerateField = false)]
        public string Email { set; get; }

        [Display(AutoGenerateField = false)]
        public string OldDisplayName { set; get; }

        [Display(AutoGenerateField = false)]
        public string NewDisplayName { set; get; }

        /// <summary>
        /// Gets and sets the password.
        /// </summary>
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [Display(Order = 0, Name = "OldPasswordLabel", Description = "PasswordDescription", ResourceType = typeof(RegistrationDataResources))]
        [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [StringLength(32, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        public string OldPassword { get; set; }

        /// <summary>
        /// Gets and sets the password.
        /// </summary>
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [Display(Order = 1, Name = "NewPasswordLabel", Description = "PasswordDescription", ResourceType = typeof(RegistrationDataResources))]
        [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [StringLength(32, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        public string NewPassword { get; set; }

        /// <summary>
        /// Gets and sets the password confirmation string.
        /// </summary>
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [Display(Order = 2, Name = "ConfirmNewPasswordLabel", ResourceType = typeof(RegistrationDataResources))]
        public string ConfirmNewPassword { set; get; }


        #region 修改密码

        public bool ChangedPassword()
        {
            try
            {
                Membership.Providers["AspNetSqlMembershipProvider"].ChangePassword(this.UserName, this.OldPassword, this.NewPassword);
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteError(string.Format("{0} Changes Password: Changed {1} to {2} is failed....", this.UserName, this.OldPassword, this.NewPassword));
                Log.WriteError(ex.Message);
                Log.WriteError(ex.StackTrace);
                return false;
            }
        }

        #endregion

        #region 更改邮箱

        public bool ChangedEamil()
        {
            try
            {
                Guid userID = Guid.Empty;
                using (var dc = PMSEntities.Create())
                {
                    userID = Guid.Parse(dc.aspnet_Users.FirstOrDefault(p => p.UserName == this.UserName).UserId);
                    if (!this.OldDisplayName.Equals(this.NewDisplayName))
                    {
                        var aspnet_Profile = dc.aspnet_Profile.FirstOrDefault(p => p.aspnet_Users.UserName == this.UserName);
                        {
                            aspnet_Profile.PropertyValuesString = this.NewDisplayName;
                            aspnet_Profile.PropertyNames = string.Format("FriendlyName:S:0:{0}:", this.NewDisplayName.Length);
                            dc.SaveChanges();
                        }
                    }
                }
                MembershipUser user = Membership.GetUser(userID);
                user.Email = this.Email;

                Membership.Providers["AspNetSqlMembershipProvider"].UpdateUser(user);
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteError(string.Format("{0} Changs Email: Changed to {1} is failed....", this.UserName, this.Email));
                Log.WriteError(ex.Message);
                Log.WriteError(ex.StackTrace);
                return false;
            }
        }

        #endregion
    }
}
